using System;
using System.Collections.Generic;
using System.Text;
using smartBrowser.Util;
using System.Threading;
using System.Text.RegularExpressions;
using smartBrowser.GUI;


namespace smartBrowser.Task
{
    /// <summary>
    /// @author Joel
    /// </summary>    
    public class FetchRDFTask : Task
    {
        public FetchRDFTask(WebSite webSite, TabPageForm form)
            : base(webSite, form)
        {  
        }

        protected override void Execute()
        {
            //vai buscar as ontologias que descrevem o site
            Console.Out.WriteLine("Fetch RDF TASK START");
            
            
           String expression = "<a.*?href=\"http://((.*?\\.owl)|(.*?\\.rdf)|(.*?\\.rdfs))\".*?>";

            Regex regex = new Regex(expression);

            MatchCollection matches = regex.Matches(webSite.Source);

            foreach (Match match in matches)
            {

                String link = match.Groups[1].Value;

                Ontology.Ontology ontologyFromSite = null;

                System.Windows.Forms.MessageBox.Show(this.AppendHTTP(link));

                Uri uri = new Uri(this.AppendHTTP(link));

                ontologyFromSite = new Ontology.Ontology(uri);

                webSite.OntologyList.Add(ontologyFromSite);

            }

            
            FireCompleteEvent();
            Console.Out.WriteLine("Fetch RDF TASK COMPLETE");
        }

        private String AppendHTTP(String url)
        {
            if (url.StartsWith("http://"))
            {
                return url;
            }
            else
            {                
                return  "http://"+url;
            }
        }
        
        
    }
}
